/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: 7.23client.c
> Author:wyb
> Mail: 
> Created Time: 2024年07月23日 星期二 15时15分14秒
***************************************************/

#include "7.23client.h"
#include "7.25client.h"
#include "7.26client.h"
#include "sys_log.h"

//申请连接
int requset_connect()
{
	int sock = 0;
	int ret = 0;
	
	//指向sockaddr_in的结构体指针，本地信息地址  绑定IP用的
	struct sockaddr_in addr;
	memset(&addr,0,sizeof(addr));
 
 	//创建套接字
 	sock = socket(AF_INET,SOCK_STREAM,0);
 	if(sock == -1)
 	{
 		LOG_E("socket");
 		exit(-1);
 	}
 
 	addr.sin_family = AF_INET;//协议族
 	addr.sin_port = htons(6666);//端口
 	addr.sin_addr.s_addr = inet_addr("39.107.122.0");//ip
 	//addr.sin_addr.s_addr = inet_addr("192.168.0.185");//ip
 	//与服务器连接，发送连接请求
 	ret = connect(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr));
 	printf("%d",ret);
	if(ret == -1)
	{
		LOG_E("connect");
		exit(-1);
	}
	return sock;
 	
}


//查看水泵
void Viewwaterpump()
{
	int num = 0;
	int num1 = 0;
	char type[30] = {"view"};

	printf("输入要查看几号水泵信息\n");
	scanf("%d",&num);
	
	//定义cJSON 指针并创建json对象
	cJSON *json = NULL;
	json = cJSON_CreateObject();
	
	//添加多个"键-值”对
	cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
	cJSON_AddItemToObject(json,"num",cJSON_CreateNumber(num));
	
	//将json对象转成json格式字符串
	char *p = cJSON_Print(json);
		
	//调申请连接函数
	int sock = 0;
	int ret = 0;	
	sock = requset_connect();
	
	//发送
	ret = send(sock,p,strlen(p),0);
	if(ret == -1)
	{
		LOG_E("send");
		exit(-1);
	
	}
	
	while(1)
	{	
		int ret1 = 0;	
		char buf[1024] = {0};
		
		struct refer info;
		memset(&info,0,sizeof(info));
		
		ret1 = recv(sock,buf,sizeof(buf),0);
		if(ret1 == -1)
		{
			LOG_E("recv");
			exit(-1);
		}
		
		printf("%s\n",buf);
		//将字符串转为json格式对象
		json = cJSON_Parse(buf);

		cJSON *node = NULL;
		//由键获取值
		node = cJSON_GetObjectItem(json,"type");
		if(node == NULL)
		{
			printf("can't find type\n");
			exit(-1);
		}
		if(node->type == cJSON_String)
		{
			strcpy(type,node->valuestring);
			
		}
		
		node = cJSON_GetObjectItem(json,"time");
		if(node == NULL)
		{
			printf("can't find  time\n");
			exit(-1);
		}
		if(node->type == cJSON_String)
		{
			strcpy(info.time,node->valuestring);
			
		}


		node = cJSON_GetObjectItem(json,"flow");
		if(node == NULL)
		{
			printf("can't find flow\n");
			exit(-1);
		}
		if(node->type == cJSON_Number)
		{
			info.flow = node->valueint;
		}

		node = cJSON_GetObjectItem(json,"temperature");
		if(node == NULL)
		{
			printf("can't find temperature\n");
			exit(-1);
		}
		if(node->type == cJSON_Number)
		{
			info.temperature = node->valueint;
		}

		node = cJSON_GetObjectItem(json,"liquidlevel");
		if(node == NULL)
		{
			printf("can't find liquidlevel\n");
			exit(-1);
		}
		if(node->type == cJSON_Number)
		{
			info.liquidlevel = node->valueint;
		}

		node = cJSON_GetObjectItem(json,"stress");
		if(node == NULL)
		{
			printf("can't find stress\n");
			exit(-1);
		}
		if(node->type == cJSON_Number)
		{
			info.stress = node->valueint;
		}

		node = cJSON_GetObjectItem(json,"valve");
		if(node == NULL)
		{
			printf("can't find  valve\n");
			exit(-1);
		}
		if(node->type == cJSON_String)
		{
			strcpy(info.valve,node->valuestring);
			
		}
			
		printf("time:%s\n",info.time);
		printf("flow:%d\n",info.flow);
		printf("liquidlevel:%d\n",info.liquidlevel);
		printf("temperature:%d\n",info.temperature);
		printf("stress:%.2f\n",info.stress);
		printf("valve:%s\n",info.valve);
		
		if(strcmp(type,"over") == 0)
		{
			printf("接受完成");
			break;
		}
	
		cJSON_Delete(json);
		sleep(1);
	}
}
//删除水泵信息
void Removalpump()
{
	int num = 0;
	int num1 = 0;
	char type[30] = {"delete"};
		
	printf("输入要删除几号水泵信息：\n");
	scanf("%d",&num);
	
	//定义cJSON 指针并创建json对象
	cJSON *json = NULL;
	json = cJSON_CreateObject();
	
	//添加多个"键-值”对
	cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
	cJSON_AddItemToObject(json,"num",cJSON_CreateNumber(num));
	
	//将json对象转成json格式字符串
	char *p = cJSON_Print(json);
		
	//调申请连接函数
	int sock = 0;
	int ret = 0;	
	sock = requset_connect();
	
	//发送
	ret = send(sock,p,strlen(p),0);
	if(ret == -1)
	{
		LOG_E("send");
		exit(-1);
	
	}
	
	//接受
	ret = recv(sock,&num1,4,0);
	if(ret == -1)
	{
		LOG_E("recv");
		exit(-1);
	}
	if(num1 == 1)
	printf("成功\n");
	if(num1 == 0)
	printf("失败\n");
}
//日志信息
void Loginformation(void)
{
	char num[20] = {0};
	char type[30] = {"Logs"};
	
	
	//定义cJSON 指针并创建json对象
	cJSON *json = NULL;
	json = cJSON_CreateObject();
	
	//添加多个"键-值”对
	cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
	
	//将json对象转成json格式字符串
	char *p = cJSON_Print(json);
		
	//调申请连接函数
	int sock = 0;
	int ret = 0;
	int ret1 = 0;
	char buf[1024] = {0};	
	sock = requset_connect();
	
	//发送
	ret = send(sock,p,strlen(p),0);
	if(ret == -1)
	{
		LOG_E("send");
		exit(-1);
	
	}
	
	int id;
	char event[20];
	char time[100];
	while(1)
	{
		//接受
		ret1 = recv(sock,buf,sizeof(buf),0);
		if(ret1 == -1)
		{
			LOG_E("recv");
			exit(-1);
		}
		//将字符串转为json格式对象
		json = cJSON_Parse(buf);

		cJSON *node = NULL;
		//由键获取值
		node = cJSON_GetObjectItem(json,"type");
		if(node == NULL)
		{
			printf("can't find type\n");
			exit(-1);
		}
		if(node->type == cJSON_String)
		{
			strcpy(type,node->valuestring);
		}
		
		node = cJSON_GetObjectItem(json,"id");
		if(node == NULL)
		{
			printf("can't find id\n");
			exit(-1);
		}
		if(node->type == cJSON_Number)
		{
			id = node->valueint;
			printf("id:%d\n",id);
		}
		
		node = cJSON_GetObjectItem(json,"event");
		if(node == NULL)
		{
			printf("can't find event\n");
			exit(-1);
		}
		if(node->type == cJSON_String)
		{
			strcpy(event,node->valuestring);
			printf("event:%s\n",event);
		}

		node = cJSON_GetObjectItem(json,"time");
		if(node == NULL)
		{
			printf("can't find time\n");
			exit(-1);
		}
		if(node->type == cJSON_String)
		{
			strcpy(time,node->valuestring);
			printf("time:%s\n",time);
		}

		if(strcmp(type,"over") == 0)
		{
			break;
		}
	}
	
}

//控制阀门开关
void Controlvalve()
{
	char type[30] = {"Switch"};
	
	struct controls da;
	memset(&da,0,sizeof(da));
	
	printf("输入控制几号开关：\n");
	scanf("%d",&da.id);
	
	printf("输入开关命令：\n");
	scanf("%s",da.valve);
	
	//定义cJSON 指针并创建json对象
	cJSON *json = NULL;
	json = cJSON_CreateObject();
	
	//添加多个"键-值”对
	cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
	cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(da.id));
	cJSON_AddItemToObject(json,"valve",cJSON_CreateString(da.valve));
	
	//将json对象转成json格式字符串
	char *p = cJSON_Print(json);
		
	//调申请连接函数
	int sock = 0;
	int ret = 0;
	int num1 = 0;
	int ret1 = 0;	
	sock = requset_connect();
	
	//发送
	ret = send(sock,p,strlen(p),0);
	if(ret == -1)
	{
		LOG_E("send");
		exit(-1);
	
	}
	
	//接受
	ret1 = recv(sock,&num1,4,0);
	if(ret1 == -1)
	{
		LOG_E("recv");
		exit(-1);
	}
	if(num1 == 1)
	printf("成功\n");
	if(num1 == 0)
	printf("失败\n");
	if(num1 == 2)
	printf("关闭\n");
}
